package com.zenika.zenbinding.client.widget;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RadioGroup extends Composite implements ClickHandler, ValueChangeHandler<Boolean> {
  
  private static int counter = 0;
  
  // FIXME does lock actually work?
  private synchronized static String getUniqueName() {
    counter++;
    return "radio" + counter;
  }
  
  private boolean enabled = true;
  private List<ValueChangeHandler<Boolean>> listeners;
  private String name;
  private Panel panel;
  private String selectedItem;
  
  private List<RadioButton> widgetList;
  
  public RadioGroup() {
    this(getUniqueName());
  }
  
  public RadioGroup(boolean vertical) {
    this(getUniqueName(), vertical);
  }
  
  public RadioGroup(String name) {
    this(name, true);
  }
  
  public RadioGroup(String name, boolean vertical) {
    this.selectedItem = "none";
    this.name = name;
    this.listeners = new ArrayList<ValueChangeHandler<Boolean>>();
    this.widgetList = new ArrayList<RadioButton>();
    
    if ( vertical )
      this.panel = new VerticalPanel();
    else
      this.panel = new HorizontalPanel();
    
    this.initWidget(this.panel);
  }
  
  public void addChangeListener(ValueChangeHandler<Boolean> listener) {
    this.listeners.add(listener);
  }
  
  public void addItem(String item) {
    RadioButton radio = new RadioButton(this.name, item);
    radio.addClickHandler(this);
    radio.addValueChangeHandler(this);
    radio.setEnabled(enabled);
    this.widgetList.add(radio);
    this.panel.add(radio);
  }
  
  public String getSelectedItem() {
    return this.selectedItem;
  }
  
  public void onClick(ClickEvent event) {
    RadioButton radio = (RadioButton) event.getSource();
    this.setItemSelected(radio.getText());
//    this.fireChange();
  }
  
  public void onValueChange(ValueChangeEvent<Boolean> event) {
    Iterator<ValueChangeHandler<Boolean>> iter = this.listeners.iterator();
    while ( iter.hasNext() ) {
      iter.next().onValueChange(event);
    }
  }
  
  public void removeChangeListener(ValueChangeHandler<Boolean> listener) {
    this.listeners.remove(listener);
  }
  
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    for ( int i = 0; i < this.widgetList.size(); i++ ) {
      RadioButton radio = (RadioButton) this.widgetList.get(i);
      radio.setEnabled(enabled);
    }
  }
  
//  private void fireChange() {
//    System.out.println(this.selectedItem);
//    System.out.println(this.selectedItem);
//    Iterator iter = this.listeners.iterator();
//    while ( iter.hasNext() ) {
//      ((ChangeListener) iter.next()).onChange(this);
//    }
//  }
  
  public void setItemSelected(String item) {
    Iterator<RadioButton> iter = this.widgetList.iterator();
    while ( iter.hasNext() ) {
      RadioButton radio = iter.next();
      String title = radio.getText();
      if ( title.equals(item) ) {
        radio.setValue(true);
        this.selectedItem = item;
        break;
      }
    }
  }
  
}
